package org.timeismoney.payment.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.timeismoney.payment.common.PageBean;
import org.timeismoney.payment.exception.BizException;
import org.timeismoney.payment.model.PaymentFin;
import org.timeismoney.payment.service.PaymentFinService;
import org.timeismoney.payment.web.WebUtils;

public class PayFinAction extends BaseAction {
	private PaymentFinService	service;

	public void setService(PaymentFinService service) {
		this.service = service;
	}

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		try {
			// rows=10&page=1
			// sidx schoolYearNo
			// sord desc
			String rows = request.getParameter("rows");
			String page = request.getParameter("page");
			String sortCol = request.getParameter("sidx");
			String sortD = request.getParameter("sord");
			int rowsNo = 10;
			try {
				rowsNo = Integer.valueOf(rows);
			} catch (RuntimeException e) {
			}
			int pageNo = 1;
			try {
				pageNo = Integer.valueOf(page);
			} catch (RuntimeException e) {
			}
			PageBean<PaymentFin> pageBean = new PageBean<PaymentFin>(
					pageNo, rowsNo, sortCol, sortD);
			
			String stuNo = request.getParameter("stuNo");
			if (StringUtils.isEmpty(stuNo)) {
				throw new BizException("请传入学号参数");
			}
			List<PaymentFin> fins = service.getFinsByStuNo(stuNo);
			pageBean.setItems(fins);
			pageBean.setTotalRows(fins.size());
			renderJson(response, WebUtils.generateJSONByPageBean(pageBean));
		} catch (Exception e) {
			log.error(e.getMessage());
			renderJson(
					response,
					WebUtils.generateErrorJSON(TEXT_ERROR_MSG_QUERY + " "
							+ e.getMessage()));
		}
		return null;
	}

}
